﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Liang.Util
{
    public class Propety
    {
        public static string GetDescription<T>(string key)
        {
            string strValue = "";
            foreach (MemberInfo mi in typeof(T).GetMembers())
            {
                if (key == mi.Name)
                {
                    
                    var tmp = mi.GetCustomAttributes(typeof(DescriptionAttribute), false);

                    if (0 < tmp.Count())
                    {
                        strValue = ((DescriptionAttribute)tmp[0]).Description;
                        break;
                    }

                    // .net 4.5 用以下方法
                    //Attribute attr = mi.GetCustomAttribute(typeof(DescriptionAttribute));
                    //if (null != attr)
                    //{
                    //    strValue = ((DescriptionAttribute)attr).Description;
                    //    break;
                    //}
                }
            }

            return strValue;
        }
    }
}
